#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexLatitudeLongitude(L"(\\d+°)(\\d+′)(\\d+″)?");	//度分秒


void TextNormalizer::TnReLatitudeLongitude(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while(regex_search(*fpInWsentence, tWsmatch, mcWregexLatitudeLongitude))
	{
		string tInSentence = wstring2utf8string(*fpInWsentence);
		wstring tWstrMatch = tWsmatch[0];	//匹配到三角函数的字符串

		size_t tPosDegree = tWstrMatch.find(L"°");
		size_t tPosMinute = tWstrMatch.find(L"′");
		size_t tPosSecond = tWstrMatch.find(L"″");

		string tStrDegree = (tPosDegree != string::npos) ? wstring2utf8string(tWstrMatch.substr(0, tPosDegree)) : "";
		string tStrMinute = (tPosMinute != string::npos) ? wstring2utf8string(tWstrMatch.substr(tPosDegree+1, tPosMinute-tPosDegree-1)) : "";
		string tStrSecond = (tPosSecond != string::npos) ? wstring2utf8string(tWstrMatch.substr(tPosMinute+1, tPosSecond-tPosMinute-1)) : "";
		
		string tStrReplace;
		if(fcInLanguage == LANG_EN) {
			if(tStrDegree != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrDegree}, RME_en_negative_decimal);
				tStrReplace += " degree";
				tStrReplace += stoi(tStrDegree)==1 ? "" : "s";
				tStrReplace += " ";
			}
			if(tStrMinute != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrMinute}, RME_en_negative_decimal);
				tStrReplace += " minute";
				tStrReplace += stoi(tStrMinute)==1 ? "" : "s";
				tStrReplace += " ";
			}
			if(tStrSecond != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrSecond}, RME_en_negative_decimal);
				tStrReplace += " second";
				tStrReplace += stoi(tStrSecond)==1 ? "" : "s";
				tStrReplace += " ";
			}
		}
		else {

			if(tStrDegree != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrDegree}, RME_zh_negative_decimal) + "度";
			}
			if(tStrMinute != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrMinute}, RME_zh_negative_decimal) + "分";
			}
			if(tStrSecond != "") {
				tStrReplace += ReplaceMethod(vector<string>{tStrSecond}, RME_zh_negative_decimal) + "秒";
			}
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
